//
//  HomeCell.m
//  敢唆嘛!
//
//  Created by 教主 on 15/11/23.
//  Copyright © 2015年 zms. All rights reserved.
//

#import "HomeCell.h"
#import "Device.h"
#import "HomeModel.h"

@interface HomeCell ()
@property(nonatomic, strong)UIImageView *recommendUserHeaderImage;          // 推荐的用户头像
@property(nonatomic, strong)UILabel *recommendUserNameLabel;                // 推荐的用户名
@property(nonatomic, strong)UILabel *recommendTime;                         // 推荐时间
@property(nonatomic, strong)UILabel *recommendInfoTitleLabel;               // 推荐标题
@property(nonatomic, strong)UIView *recommendBackgroundView;                // 创建主题灰色背景
@property(nonatomic, strong)UIImageView *userHeaderImage;                   // 用户头像
@property(nonatomic, strong)UILabel *userNameLabel;                         // 用户名
@property(nonatomic, strong)UIImageView *typeImage;                         // 类型图标
@property(nonatomic, strong)UILabel *typeLabel;                             // 类型
@property(nonatomic, strong)UILabel *infoTitleLabel;                        // 内容标题
@property(nonatomic, strong)UILabel *infoLabel;                             // 内容
@property(nonatomic, strong)UIView *downView;                               // 装载三个评论数的view
@property(nonatomic, strong)UILabel *recommendNumberLabel;                  // 推荐数
@property(nonatomic, strong)UILabel *collectNumberLabel;                    // 收藏数
@property(nonatomic, strong)UILabel *commentNumberLabel;                    // 评论数
//@property(nonatomic, strong)UIView *blackXian;                              // 尾部黑色细线

@end

@implementation HomeCell
{
    CGFloat x;
    CGFloat y;
    CGFloat sx;
    CGFloat sy;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        x = KSWidth / 19;
        y = KSWidth / 19;
        sx = KSWidth / 25;
        sy = KSWidth / 25;
        
        // 创建推荐的用户头像
        self.recommendUserHeaderImage = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, KSWidth / 10, KSWidth / 10)];
        self.recommendUserHeaderImage.layer.cornerRadius = KSWidth / 20;
        self.recommendUserHeaderImage.layer.masksToBounds = YES; // 添加遮罩
        [self.contentView addSubview:_recommendUserHeaderImage];
        
        // 创建推荐的用户名
        CGFloat recommendUserHeaderImageWidth = self.recommendUserHeaderImage.frame.size.width;
        self.recommendUserNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(recommendUserHeaderImageWidth + x * 2 , y, KSWidth - recommendUserHeaderImageWidth - x * 4, KSWidth / 20)];
        self.recommendUserNameLabel.alpha = 0.8;
        self.recommendUserNameLabel.textColor = [UIColor blackColor];
        self.recommendUserNameLabel.font = [UIFont systemFontOfSize:KSWidth / 28];
        self.recommendUserNameLabel.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_recommendUserNameLabel];
        
        // 创建推荐时间
        CGFloat recommendUserNameLabelHeight = self.recommendUserNameLabel.frame.size.height;
        self.recommendTime = [[UILabel alloc] initWithFrame:CGRectMake(recommendUserHeaderImageWidth + x * 2 , y + recommendUserNameLabelHeight, KSWidth - recommendUserHeaderImageWidth - x * 4, KSWidth / 20.8)];
        self.recommendTime.textColor = [UIColor blackColor];
        self.recommendTime.alpha = 0.6;
        self.recommendTime.font = [UIFont systemFontOfSize:KSWidth / 34];
        self.recommendTime.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_recommendTime];
        
        // 创建推荐标题内容
        self.recommendInfoTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, recommendUserHeaderImageWidth + y * 1.8, KSWidth - x * 2, KSWidth / 22)];
        self.recommendInfoTitleLabel.textColor = [UIColor blackColor];
        self.recommendInfoTitleLabel.alpha = 0.8;
        self.recommendInfoTitleLabel.font = [UIFont systemFontOfSize:KSWidth / 28];
        self.recommendInfoTitleLabel.textAlignment = NSTextAlignmentLeft;
        self.recommendInfoTitleLabel.numberOfLines = 10;    // 最多10行
        [self.contentView addSubview:_recommendInfoTitleLabel];
        
        // 创建主题灰色背景
        CGFloat recommendInfoTitleLabelHeight = self.recommendInfoTitleLabel.frame.size.height;
        self.recommendBackgroundView = [[UIView alloc] initWithFrame:CGRectMake((KSWidth - (KSWidth - x * 2)) / 2, recommendUserHeaderImageWidth + recommendInfoTitleLabelHeight + y * 5.5, KSWidth - x * 2, KSWidth - x * 5)];
        self.recommendBackgroundView.backgroundColor = [UIColor colorWithRed:245 / 255.0 green:245 / 255.0 blue:245 / 255.0 alpha:1.0];
        [self.contentView addSubview:_recommendBackgroundView];
        
//        // 尾部黑色细线
//        CGFloat recommendBackgroundViewHeight = self.recommendBackgroundView.frame.size.height;
//        self.blackXian = [[UIView alloc] initWithFrame:CGRectMake(0, recommendUserHeaderImageWidth +recommendInfoTitleLabelHeight + recommendBackgroundViewHeight + y * 7.5, KSWidth, 0.5)];
//        self.blackXian.backgroundColor = [UIColor blackColor];
//        self.blackXian.alpha = 0.2;
//        [self.contentView addSubview:_blackXian];
        
        // 创建用户头像
        self.userHeaderImage = [[UIImageView alloc] initWithFrame:CGRectMake(sx, sy, KSWidth / 19, KSWidth / 19)];
        self.userHeaderImage.layer.cornerRadius = KSWidth / 37;
        self.userHeaderImage.layer.masksToBounds = YES; // 添加遮罩
        [self.recommendBackgroundView addSubview:_userHeaderImage];
        
        // 创建用户名
        CGFloat userHeaderImageWidth = self.userHeaderImage.frame.size.width;
        CGFloat recommendBackgroundViewWidth = self.recommendBackgroundView.frame.size.width;
        self.userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(userHeaderImageWidth + sx * 2, sy, recommendBackgroundViewWidth - userHeaderImageWidth - sx * 7, KSWidth / 19)];
        self.userNameLabel.alpha = 0.8;
        self.userNameLabel.textColor = [UIColor blackColor];
        self.userNameLabel.font = [UIFont systemFontOfSize:KSWidth / 28];
        self.userNameLabel.textAlignment = NSTextAlignmentLeft;
        [self.recommendBackgroundView addSubview:_userNameLabel];
        
        // 创建类型图标
        CGFloat userNameLabelWidth = self.userNameLabel.frame.size.width;
        self.typeImage = [[UIImageView alloc] initWithFrame:CGRectMake(userHeaderImageWidth + userNameLabelWidth + sx * 3, sy, KSWidth / 26, KSWidth / 26)];
        [self.recommendBackgroundView addSubview:_typeImage];
        
        // 创建类型
        CGFloat typeImageWidth = self.typeImage.frame.size.width;
        self.typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(userHeaderImageWidth + userNameLabelWidth + typeImageWidth + sx * 3.5, sy, KSWidth - userHeaderImageWidth - userNameLabelWidth - typeImageWidth - sx * 7, KSWidth / 26)];
        self.typeLabel.textColor = [UIColor blackColor];
        self.typeLabel.alpha = 0.6;
        self.typeLabel.font = [UIFont systemFontOfSize:KSWidth / 34];
        [self.recommendBackgroundView addSubview:_typeLabel];
        
        // 创建内容标题
        CGFloat userHeaderImageHeight = self.userHeaderImage.frame.size.height;
        self.infoTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(sx, userHeaderImageHeight + sy * 2, recommendBackgroundViewWidth - sy * 2, KSWidth / 20)];
        self.infoTitleLabel.alpha = 0.9;
        self.infoTitleLabel.textColor = [UIColor blackColor];
        self.infoTitleLabel.font = [UIFont systemFontOfSize:KSWidth / 20];
        [self.recommendBackgroundView addSubview:_infoTitleLabel];
        
        // 创建内容
        CGFloat infoTitleLabelHeight = self.infoTitleLabel.frame.size.height;
        self.infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(sx, userHeaderImageHeight + infoTitleLabelHeight + sy * 3, recommendBackgroundViewWidth - sx * 2, KSWidth / 20)];
        self.infoLabel.textColor = [UIColor blackColor];
        self.infoLabel.alpha = 0.6;
        self.infoLabel.numberOfLines = 6; // 最多6行
        self.infoLabel.font = [UIFont systemFontOfSize:KSWidth / 28];
        self.infoLabel.textAlignment = NSTextAlignmentLeft;
        [self.recommendBackgroundView addSubview:_infoLabel];
        
        // 创建装载三个评论数的view
        CGFloat infoLabelHeight = self.infoLabel.frame.size.height;
        self.downView = [[UIView alloc] initWithFrame:CGRectMake(KSWidth / 2.3, userHeaderImageHeight + infoTitleLabelHeight + infoLabelHeight + sy * 4, KSWidth - (KSWidth / 2.3) - sy * 4, KSWidth / 29)];
//        self.downView.backgroundColor = [UIColor redColor];
        [self.recommendBackgroundView addSubview:_downView];

        // 创建推荐数图标
        UIImageView *recommendNumberImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KSWidth / 27, KSWidth / 27)];
        recommendNumberImage.image = [UIImage imageNamed:@"iconfont-xunhuan2"];
        [self.downView addSubview:recommendNumberImage];
        
        // 创建推荐数
        CGFloat recommendNumberImageWidth = recommendNumberImage.frame.size.width;
        self.recommendNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + 5, 0, KSWidth / 14, KSWidth / 27)];
        self.recommendNumberLabel.alpha = 0.4;
        self.recommendNumberLabel.textColor = [UIColor blackColor];
        self.recommendNumberLabel.font = [UIFont systemFontOfSize:KSWidth / 31];
        [self.downView addSubview:_recommendNumberLabel];
        
        // 创建收藏图标
        CGFloat recommendNumberLabelWidth = self.recommendNumberLabel.frame.size.width;
        UIImageView *collectNumberImage = [[UIImageView alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + recommendNumberLabelWidth + sx, 0, KSWidth / 27, KSWidth / 27)];
        collectNumberImage.image = [UIImage imageNamed:@"iconfont-xingxing"];
        [self.downView addSubview:collectNumberImage];
        
        // 创建收藏数
        CGFloat collectNumberImageWidth = collectNumberImage.frame.size.width;
        self.collectNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + recommendNumberLabelWidth + collectNumberImageWidth + sx + 5, 0, KSWidth / 14, KSWidth / 27)];
        self.collectNumberLabel.alpha = 0.4;
        self.collectNumberLabel.textColor = [UIColor blackColor];
        self.collectNumberLabel.font = [UIFont systemFontOfSize:KSWidth / 31];
        [self.downView addSubview:_collectNumberLabel];
        
        // 创建评论图标
        CGFloat collectNumberLabelWidth = self.collectNumberLabel.frame.size.width;
        UIImageView *commentNumberImage = [[UIImageView alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + recommendNumberLabelWidth + collectNumberImageWidth + collectNumberLabelWidth + sx * 2, 0, KSWidth / 27, KSWidth / 27)];
        commentNumberImage.image = [UIImage imageNamed:@"iconfont-duihuaqipao"];
        [self.downView addSubview:commentNumberImage];
        
        // 创建评论数
        CGFloat commentNumberImageWidth = commentNumberImage.frame.size.width;
        self.commentNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + recommendNumberLabelWidth + collectNumberImageWidth + collectNumberLabelWidth + commentNumberImageWidth + sx * 2 + 5, 0, KSWidth / 14, KSWidth / 27)];
        self.commentNumberLabel.alpha = 0.4;
        self.commentNumberLabel.textColor = [UIColor blackColor];
        self.commentNumberLabel.font = [UIFont systemFontOfSize:KSWidth / 31];
        [self.downView addSubview:_commentNumberLabel];
        
    }
    return self;
}

// 传值
- (void)setModel:(HomeModel *)model
{
    // 真事数据
    [self.recommendUserHeaderImage sd_setImageWithURL:[NSURL URLWithString:model.recommendUserHeader]];
    self.recommendUserNameLabel.text = model.recommendUserName;
    self.recommendTime.text = [[PrettyDate alloc] prettyDateWithReference:model.createdAt];;
    self.recommendInfoTitleLabel.text = model.recommendInfoTitle;

    // 动态返回推荐标题高度
    CGRect recommendInfoTitleLabelBounds = [self.recommendInfoTitleLabel.text boundingRectWithSize:CGSizeMake(KSWidth - x * 2, (KSHeight - 64) / 2) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:KSWidth / 28] forKey:NSFontAttributeName] context:nil];
    
//    // 判断是否高于超过1行 超过1行就设置行间距
//    if (self.recommendInfoTitleLabel.frame.size.height > KSWidth / 20)
//    {
//        // 设置行间距
//        [self setAttributedString:self.recommendInfoTitleLabel line:5];
//    }
    
    // 计算推荐标题内容的动态高度
    self.recommendInfoTitleLabel.frame = CGRectMake(x, self.recommendUserHeaderImage.frame.size.width + y * 1.8, KSWidth - x * 2, recommendInfoTitleLabelBounds.size.height);
    
    [self.userHeaderImage sd_setImageWithURL:[NSURL URLWithString:model.userHeader]];
    self.userNameLabel.text = model.userName;
    self.typeLabel.text = model.type;
    
    // 判断类型 来给图片
    if ([self.typeLabel.text isEqualToString:@"真事"])
    {
        self.typeImage.image = [UIImage imageNamed:@"iconfont-yuanquan"];
    }
    else if ([self.typeLabel.text isEqualToString:@"创作"])
    {
        self.typeImage.image = [UIImage imageNamed:@"iconfont-yuanquan-3"];
    }
    else if ([self.typeLabel.text isEqualToString:@"游记"])
    {
        self.typeImage.image = [UIImage imageNamed:@"iconfont-yuanquan-2"];
    }
    else if ([self.typeLabel.text isEqualToString:@"秘密"])
    {
        self.typeImage.image = [UIImage imageNamed:@"iconfont-yuanquan-1"];
    }
    
    self.infoTitleLabel.text = model.infoTitle;
    self.infoLabel.text = model.info;
    
    // 动态返回被推荐的内容高度
    CGRect infoLabelBounds = [self.infoLabel.text boundingRectWithSize:CGSizeMake(self.recommendBackgroundView.frame.size.width - sx * 2, (KSHeight - 64) / 5) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:KSWidth / 34] forKey:NSFontAttributeName] context:nil];
    
//    // 判断是否高于超过1行 超过1行就设置行间距
//    if (self.infoLabel.frame.size.height > KSWidth / 22)
//    {
//        //        设置行间距
//        [self setAttributedString:self.infoLabel line:5];
//    }
    
    // 重置被推荐的内容高度
    self.infoLabel.frame = CGRectMake(sx, self.userHeaderImage.frame.size.height + self.infoTitleLabel.frame.size.height + sy * 3, self.recommendBackgroundView.frame.size.width - sx * 2, infoLabelBounds.size.height);
    
    // 计算装载三个评论数的view的动态高度
    self.downView.frame = CGRectMake(KSWidth / 2.3, self.userHeaderImage.frame.size.height + self.infoTitleLabel.frame.size.height + self.infoLabel.frame.size.height + sy * 4, KSWidth - (KSWidth / 2.3) - sy * 4, KSWidth / 29);
    
    self.recommendNumberLabel.text = [NSString stringWithFormat:@"%ld",model.recommendNumber];
    self.collectNumberLabel.text = [NSString stringWithFormat:@"%ld",model.collectNumber];
    self.commentNumberLabel.text = [NSString stringWithFormat:@"%ld",model.commentNumber];
    
    // 计算主题灰色背景的动态高度
    CGFloat recommendInfoTitleLabelHeight = self.recommendInfoTitleLabel.frame.size.height;
    self.recommendBackgroundView.frame = CGRectMake((KSWidth - (KSWidth - x * 2)) / 2, recommendInfoTitleLabelHeight + y * 4.5, KSWidth - x * 2, self.userHeaderImage.frame.size.height + self.infoTitleLabel.frame.size.height + self.infoLabel.frame.size.height + self.downView.frame.size.height + sy * 5);
//    // 计算黑线的动态高度
//    self.blackXian.frame = CGRectMake(0, recommendInfoTitleLabelHeight + self.recommendBackgroundView.frame.size.height + y * 5.5, KSWidth, 0.5);
    
    

}

// 动态返回动态cell
+ (CGFloat)heightForCell:(HomeModel *)model
{
    // 动态返回高度
    CGFloat x = KSWidth / 19;
    CGFloat sx = KSWidth / 25;
    CGRect recommendInfoTitleLabelBounds = [model.recommendInfoTitle boundingRectWithSize:CGSizeMake(KSWidth - x * 2, (KSHeight - 64) / 2) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:KSWidth / 28] forKey:NSFontAttributeName] context:nil];
    // 动态返回被推荐的内容高度
    CGRect infoLabelBounds = [model.info boundingRectWithSize:CGSizeMake((KSWidth - x * 2) - sx * 2, (KSHeight - 64) / 5) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:KSWidth / 34] forKey:NSFontAttributeName] context:nil];
    
    CGFloat height = x + (KSWidth / 10) + x + x + recommendInfoTitleLabelBounds.size.height + x + x + sx + (KSWidth / 19) + sx + (KSWidth / 20) + sx + 2 + infoLabelBounds.size.height + (KSWidth / 29);
    
    return height;
}

// 设置行间距
- (void)setAttributedString:(UILabel *)label line:(CGFloat)line
{
    //        设置行间距
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:line];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, label.text.length)];
    label.attributedText = attributedString;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
